<?php

header("Content-type:text/html;Charset=utf8");

$ck = file_get_contents('cookie');
$token = file_get_contents('token');

$header = array();
$header[] = 'X-Csrf-Token:' . $token;
$header[] = 'Accept:application/json, text/javascript, */*; q=0.01';
$header[] = 'application/x-www-form-urlencoded; charset=UTF-8';
$header[] = 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82';

function post_url($url, $fields)
{
    global $header, $ck;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    if (is_array($fields)){
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);// 启用时会将头文件的信息作为数据流输出
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_COOKIE, $ck);
    $content = curl_exec($ch);
    return $content;
}

function add()
{
    $url = 'http://wap.kaboshihaoka.com/bu/agent/add';
    $fields = [
        'nickname' => '黄观如',
        'account' => 'huanggru',
        'password' => 'Kbs10010',
        'note' => '',
        'package_agent_id' => '0'
    ];
    return post_url($url, $fields);
}

function profitall($id)
{
    $url = 'http://wap.kaboshihaoka.com/bu/agent/profitall';
    $fields = ['agent_id' => $id, 'profit' => 10];
    return post_url($url, $fields);
}

function getid($phone){
    $url = "http://wap.kaboshihaoka.com/bu/agent/list?agent_name=$phone";
//    $content = file_get_contents($url);

    $content = post_url($url, '$fields');

//    echo $content;
    $table_data = preg_match_all('/<table[^>]*>(.*?) <\/table>/si',$content,$match);
    $table_data = $match[0][0];
    $table_array = explode('<tr>',$table_data);
    $data = array();

    for($i=2;$i<count($table_array);$i++){
        $data[$i] = explode('</td>',$table_array[$i]);
        for($j = 0;$j<count($data[$i]);$j++){
            $data[$i][$j] = preg_replace('/\s(?=\s)/','',trim(strip_tags($data[$i][$j])));
        }
        array_pop($data[$i]);
    }
    var_dump($data);
    return $data[2][0]??0;
}

//echo add();
$ids = [37514, 37528, 37735, 37898, 37903, 37962, 37984, 38059, 38075, 38273,38284,38287,38318,38411,38518,38565,
    38657,38823,38883,38939,39032,39090,39254,39302,39408,39448,39483,39514,39822,39825,39829,];
if ($_GET['id']){
    echo profitall($_GET['id']);
}else if ($_GET['phone']) {
    echo getid($_GET['phone']);
}else{
    foreach ($ids as $id){
        echo profitall($id);
        echo '<br>';
        sleep(0.1);
    }
}


